package 树;

import 抽象数据类型.TreeNode;

import java.util.Stack;

/**
 * @description: 后缀表达树
 * @author: ywk
 * @date: 2020-07-09
 */
public class SuffixTree {
    public static TreeNode buildTreeNode(String string) {
        Stack<TreeNode> stack = new Stack<>();
        for (int i = string.length() - 1; i >= 0; i--) {
            stack.push(new TreeNode(string.charAt(i)));
        }

        Stack<TreeNode> stack1 = new Stack<>();
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            if (isOp((Character) pop.val)) {
                pop.setLeft(stack1.pop());
                pop.setRight(stack1.pop());
            }
            stack1.push(pop);
        }

        return stack1.peek();
    }

    public static boolean isOp(char c) {
        String str = "/*+-";
        return str.indexOf(c) != -1;
    }

    public static void main(String[] args) {
        String str = "1234-++";
        System.out.println(buildTreeNode(str));
    }
}
